package org.mg.engine;

import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.mg.engine.core.Session;
import org.msg.commons.RespStatus;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class InstructAbstract implements Instruct{

    public static volatile Map<String,Session> CACHE = new ConcurrentHashMap<>();

    public static volatile Multimap<Session, String> multimap = ArrayListMultimap.create();


    private Session session;

    private PrintWriter out;

    @Override
    public abstract void execute(String data);

    @Override
    public void println(String data) {
        out.println(data);
    }

    @Override
    public Instruct init(Session session) throws IOException {
        this.session = session;
        this.out = new PrintWriter(this.session.getSocket().getOutputStream(),true);
        return this;
    }

    protected boolean validata(String params) {
        if(Strings.isNullOrEmpty(params)) {
            error();
            return false;
        }
        return true;
    }

    public Session getSession() {
        return this.session;
    }

    protected void error() {
        println(RespStatus.ERROR);
    }

    protected void success() {
        println(RespStatus.SUCCESS);
    }
}
